<?php

namespace App\Models;

use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Config extends Model {
    protected  $table ='config';

    public  $guarded = [];

    public  $timestamps = true;

    /**
     * 添加
     * @data int $ 数据
     */
    public static function add($data)
    {
        Log::info('添加配置数据:', $data);
        $save_data =  [
            'name' => $data['name'],
            'key' => $data['key'],
            'value' => $data['value'],
            'status' => 1,
        ];
        DB::beginTransaction();
        try {
            $check = self::where('key', $data['key'])->first();
            if ($check) return result_json(409, '主键已经存在');
            $save = self::create($save_data);
            if (!$save) {
                return result_json(500, '失败');
            }
            DB::commit();
            return result_json(200, '成功');
        } catch (Exception $e) {
            Log::error("发布商品失败 Caught exception::",  $e->getMessage(),'<br>');
            return result_json(500, '失败');
        }
    }

    /**
     * 编辑
     * @data int $ 数据
     */
    public static function edit($data)
    {
        Log::info('编辑配置数据:', $data);
        $save_data =  [
            'name' => $data['name'],
            'value' => $data['value'],
            'status' => 1,
        ];
        try {
            $save = self::where(['id' => $data['id']])->update($save_data);
            if (!$save) result_json(500, '失败');
            return result_json(200, '成功');
        } catch (Exception $e) {
            Log::error("编辑配置 Caught exception::",  $e->getMessage(),'<br>');
            return result_json(500, '失败');
        }
    }

    /**
     * 获取列表
     * @param $data
     * @return array|string
     */
    public static function getList($data)
    {
        $sort = !isset($data['sort']) ? [] : (is_array(json_decode($data['sort'], true)) ? json_decode($data['sort'], true) : []);
        $list = self::when(isset($data['keywords']) && !empty($data['keywords']), function ($query) use ($data) {
            $query->where('name', 'like', '%' . $data['keywords'] . '%');
            $query->orWhere('key', 'like', '%' . $data['keywords'] . '%');
            return $query;
        })
        ->when($sort, function ($query) use ($sort) {
            foreach ($sort as $k => $vo) {
                $query->orderBy($k, $vo);
            }
            return $query;
        })
            ->orderBy('id', 'desc')
            ->paginate($data['limit']);
        return $list;
    }

    /**
     * 获取信息
     * @param $data
     * @return array|string
     */
    public static function getInfo($data)
    {
        $info = self::where(['id' => $data['id']])->first();
        if (!$info) return ['id' => 0, 'name' => "", 'key' => '', 'value' => ''];
        $info = $info->toArray();
        return $info;
    }

    /**
     * 删除
     * @data int $ 数据
     */
    public static function del($data)
    {
        Log::info('删除配置项:', $data);
        try {
            $list = self::where('id', $data['id'])->delete();
            return result_json(200, '成功');
        } catch (Exception $e) {
            Log::error("删除配置项 Caught exception::", $e->getMessage(), '<br>');
            return result_json(500, '失败');
        }
    }

}